package com.m4u.hibernate.test.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil
{
    
    /**
     * Fabrica Global de Sessões - singleton
     */
    private static final SessionFactory sessionFactory;
    
    //TESTEEEEEEEEEEEEEEEEEEEEEEE
    /**
     * Bloco estático (inicialização no class load na JVM)
     * Inicia a session factory global
     */
    static
    {

        try {
            //Inicia a SessionFactory com as configuracoes do hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        }
        catch(Throwable e) {
            
            DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = dateFormatter.format(new Date(System.currentTimeMillis()));

            System.err.println(date+" [ERROR] Erro ao instanciar a sessionFactory global do Hibernate");
        
            throw new ExceptionInInitializerError(e);
        }
    }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
}
